package com.jsmile.cloud.accountcenter.shop.feign;

import javax.validation.Valid;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jsmile.cloud.accountcenter.api.req.shop.ReqSupplyCreate;
import com.jsmile.cloud.accountcenter.api.req.shop.ReqSupplyDelete;
import com.jsmile.cloud.accountcenter.api.req.shop.ReqSupplyQuery;
import com.jsmile.cloud.accountcenter.api.req.shop.ReqSupplyUpdate;
import com.jsmile.cloud.accountcenter.api.res.ResShop;
import com.jsmile.cloud.accountcenter.api.res.ResSupply;
import com.jsmile.cloud.accountcenter.shop.service.SupplyApplicationService;
import com.jsmile.mail.security.controller.AuthcController;
import com.jsmile.mail.security.handler.JsmileContext;
import com.jsmile.mall.api.JSmileResult;
import com.jsmile.mall.api.annotation.CurrentContext;
import com.jsmile.mall.api.constants.JSmileConstant;
import com.jsmile.mall.api.to.JsmilePage;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
@AllArgsConstructor
public class SupplyServiceApiImpl extends AuthcController {

    private final SupplyApplicationService supplyApplicationService;

    @RequestMapping(value = "/feign/supply/getSupply", method = RequestMethod.POST)
    public JSmileResult getSupply(@RequestBody @Valid ReqSupplyDelete reqSupplyDelete, @CurrentContext JsmileContext jsmileContext) {
        ResSupply supply = supplyApplicationService.getSupply(reqSupplyDelete.getSupplyId());
        return JSmileResult.success(supply);
    }

    @RequestMapping(value = "/feign/supply/addSupply", method = RequestMethod.POST)
    public JSmileResult addSupply(@RequestBody @Valid ReqSupplyCreate reqSupplyCreate, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.ACCOUNT_CENTER);
        reqSupplyCreate.setCurTenantId(jsmileContext.getTenantId());
        ResSupply supply = supplyApplicationService.addSupply(reqSupplyCreate);
        return JSmileResult.success(supply);
    }

    @RequestMapping(value = "/feign/supply/updateSupply", method = RequestMethod.POST)
    public JSmileResult updateSupply(@RequestBody @Valid ReqSupplyUpdate reqSupplyUpdate, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.ACCOUNT_CENTER);
        reqSupplyUpdate.setCurTenantId(jsmileContext.getTenantId());
        ResSupply supply = supplyApplicationService.updateSupply(reqSupplyUpdate);
        return JSmileResult.success(supply);
    }

    @RequestMapping(value = "/feign/supply/getSupplyPageList", method = RequestMethod.POST)
    public JSmileResult<JsmilePage<ResShop>> getSupplyPageList(@RequestBody @Valid ReqSupplyQuery reqSupplyQuery, @CurrentContext JsmileContext jsmileContext) {
        reqSupplyQuery.setCurTenantId(jsmileContext.getTenantId());
        IPage<ResSupply> pages = supplyApplicationService.getSupplyPageList(reqSupplyQuery);
        return JSmileResult.success(new JsmilePage(reqSupplyQuery.getPageIndex(), reqSupplyQuery.getPageSize(), pages.getTotal(), pages.getRecords()));
    }

}
